<template>
  <div class="companysList">
    <el-card>
      <!-- 表单数据 -->
      <el-form label-width="80px" class="top">
        <div class="input">
          <el-form-item label="企业编号">
            <el-input v-model.trim="form.eid" />
          </el-form-item>
          <el-form-item label="企业名称">
            <el-input v-model.trim="form.name" />
          </el-form-item>
          <el-form-item label="创建者">
            <el-input v-model.trim="form.username" />
          </el-form-item>
          <el-form-item label="状态" label-width="55px">
            <el-select v-model="form.status" placeholder="请选择">
              <el-option :value="0" label="禁用">禁用</el-option>
              <el-option :value="1" label="启用">启用</el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="btns">
          <div class="btn">
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button @click="clear">清除</el-button>
            <el-button type="primary" @click="submit">+新增企业</el-button>
          </div>
        </div>
      </el-form>
    </el-card>
    <el-card>
      <!-- 表格数据 -->
      <el-table :data="companyslist" :cell-style="cellStyle">
        <el-table-column label="序号" width="50px">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.limit + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="企业编号" prop="eid" />
        <el-table-column
          label="企业名称"
          width="300px"
          prop="name"
        />
        <el-table-column
          label="创建者"
          width="300px"
          prop="username"
        />
        <el-table-column
          label="状态"
          width="150px"
          prop="status"
          :formatter="formatterState"
          class="tr-red"
        />
        <el-table-column label="操作" width="300px">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="editEvent(row)">编辑</el-button>
              <el-button
                v-if="row.status === 0"
                type="text"
                class="disabled"
                @click="disableEvent(row)"
              >启用</el-button>
              <el-button
                v-else
                type="text"
                @click="disableEvent(row)"
              >禁用</el-button>
              <el-button type="text" @click="delEvent(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页器 -->
    <div class="pagination">
      <el-pagination
        layout="total,sizes,pager,prev,next,jumper"
        :total="total"
        :page-sizes="[1, 10, 20, 30, 40]"
        :page-size.sync="page.limit"
        :current-page.sync="page.page"
        @current-change="currentChange"
        @size-change="sizeChange"
      />
    </div>
    <!-- 弹框组件 -->
    <Add
      ref="add"
      :total-data="totalData"
      @getData="getData"
      @getTotalData="getTotalData"
    />
  </div>
</template>

<script>
// 引入API接口
import {
  enterpriseList,
  enterpriseRemove,
  enterpriseStatus
} from '@/api/companysList'
// 引入枚举
import company from '@/api/constant/company'
// 引入组件
import Add from './components/add.vue'
// 引入 jsCookie
import jsCookie from 'js-cookie'
export default {
  components: {
    Add
  },
  data() {
    return {
      // 表单数据
      form: {
        eid: '',
        name: '',
        username: '',
        status: ''
      },
      // 获取公司列表数据
      companyslist: [],
      // 获取全部列表数据
      totalData: [],
      // 页容量
      total: 50,
      // 页码、页容量
      page: {
        page: 1,
        // 页容量持久化
        limit: +jsCookie.get('companysList') || 10
      }
    }
  },
  created() {
    this.getData()
    this.getTotalData()
  },
  methods: {
    // 获取公司列表数据
    async getData() {
      const res = await enterpriseList({
        page: String(this.page.page),
        limit: String(this.page.limit)
      })
      console.log('企业列表', res)
      this.companyslist = res.data.data.items
      this.total = res.data.data.pagination.total
    },
    // 获取全部列表数据
    async getTotalData() {
      const res = await enterpriseList({
        page: '1',
        limit: '9999'
      })
      console.log('getTotalData', res)
      this.totalData = res.data.data.items
    },
    // 当前页改变事件
    currentChange() {
      this.getData()
    },
    // 页容量改变事件
    sizeChange() {
      this.getData()
      // 页容量持久化
      jsCookie.set('companysList', this.page.limit)
      this.page.page = 1
    },
    // 状态值转换
    formatterState(row, column, cellVal) {
      const findItem = company.stateType.find(item => item.id === cellVal)
      return findItem ? findItem.value : '-'
    },
    // 添加功能
    submit() {
      // 打开弹框
      this.$refs.add.isShow = true
      // 区分弹框类型
      this.$refs.add.mode = 'add'
    },
    // 编辑功能
    editEvent(row) {
      // 打开弹框
      this.$refs.add.isShow = true
      // 区分弹框类型
      this.$refs.add.mode = 'edit'
      // 数据回显
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    //  删除功能
    delEvent(row) {
      this.$confirm('您确定删除该条数据吗?', '温馨提示')
        .then(async() => {
          const res = await enterpriseRemove(row)
          console.log(res)
          this.$message.success('删除成功')
          // 删除后页码处理
          if (this.page.page > 1 && this.page.limit === 1) {
            this.page.page--
          }
          // 刷新列表
          this.getData()
        })
        .catch(() => {})
    },
    // 禁用功能
    async disableEvent(row) {
      const res = await enterpriseStatus(row)
      console.log(res)
      this.$message.success('设置状态成功')
      // 刷新列表
      this.getData()
    },
    // 搜索功能
    async search() {
      if (this.page.page !== 1) {
        this.page.page = 1
      }
      const res = await enterpriseList({ ...this.form, ...this.page })
      console.log(res)
      this.companyslist = res.data.data.items
      this.total = res.data.data.pagination.total
    },
    // 清除功能
    clear() {
      // 表单重置
      this.form = {
        eid: '',
        name: '',
        username: '',
        status: ''
      }
      // 重新列表数据
      this.getData()
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      // 状态列字体颜色
      if (row.status === 0 && columnIndex === 4) {
        return 'color: #EA1B29'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.companysList {
  margin: 20px;
  .top {
    display: flex;
    justify-content: left;
  }
  .input {
    display: flex;
    justify-content: left;
  }
  .btns {
    padding-left: 10px;
  }
  .btn {
    display: flex;
    justify-content: right;
  }
  .pagination {
    background-color: #fff;
    height: 80px;
    text-align: center;
    padding-top: 20px;
  }
  ::v-deep .el-table .tr-red {
    color: red !important;
  }
}
</style>
